package com.next.aap.server.tasks;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.next.aap.client.DonationSummary;
import com.next.aap.messages.NewFacebookNotificationMessage;
import com.next.aap.messages.NotificationMessage;
import com.next.aap.server.cache.DonationCache;
import com.next.aap.server.cache.FacebookCache;
import com.next.aap.server.services.AppService;

@Component
public class DonationTask {

	@Autowired
	private AppService appService;
	@Autowired
	private TaskManager taskManager;
	@Autowired
	private DonationCache donationCache;

	

	private final String baseUrl = "http://54.214.252.11/swaraj/rest/stats/";
	private final String dayDonationUrl = baseUrl+"date";
	Logger logger = LoggerFactory.getLogger(this.getClass());
	List<String> previousList = new ArrayList<String>();
	@Scheduled(fixedDelay=1000 * 60 * 20)//Every 10 minutes
	public void runDonationInfoFetchTask(){
		try{
			DonationSummary donationSummary = new DonationSummary();
			HttpClient httpClient = new DefaultHttpClient();
			loadDayDonation(httpClient,donationSummary);
			loadTotalDonation(httpClient, donationSummary);
			loadMaxDonation(httpClient, donationSummary);
			loadTotalDonors(httpClient, donationSummary);
			donationCache.setDonationSummary(donationSummary);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public static void main(String[] args){
		DonationTask donationTask = new DonationTask();
		donationTask.runDonationInfoFetchTask();
		System.out.println(donationTask.formatAmount(1234567890.0));
	}
	
	private void loadDayDonation(HttpClient httpClient,DonationSummary donationSummary) throws ClientProtocolException, IOException, JSONException{
		String dayDonationString = getResponse(httpClient, dayDonationUrl);
		JSONArray jsonArray = new JSONArray(dayDonationString);
		int size = jsonArray.length();
		if(size > 2){
			JSONArray yesterdayDonationJsonArray = (JSONArray)jsonArray.get(size-2);
			Double yesterdayDonation = yesterdayDonationJsonArray.getDouble(1);
			String yesterdayDonationString = formatAmount(yesterdayDonation);
			System.out.println("Yesterday amount="+yesterdayDonationString);
			donationSummary.setYesterdayDonation(yesterdayDonationString);
			
		}
		if(size > 1){
			JSONArray todayDonationJsonArray = (JSONArray)jsonArray.get(size-1);
			Double todayDonation = todayDonationJsonArray.getDouble(1);
			String todayDonationString = formatAmount(todayDonation);
			System.out.println("Today amount="+todayDonationString);
			donationSummary.setTodayDonation(todayDonationString);
		}
	}
	DecimalFormat myFormatter = new DecimalFormat("Rs ##,##,##,###.##");
	private String formatAmount(Double amount){
		return myFormatter.format(amount);
	}
	private void loadTotalDonation(HttpClient httpClient,DonationSummary donationSummary) throws ClientProtocolException, IOException, JSONException{
		donationSummary.setTotalDonation("Coming Soon");
		donationSummary.setTotalDonationYesterday("Coming Soon");
	}
	private void loadMaxDonation(HttpClient httpClient,DonationSummary donationSummary) throws ClientProtocolException, IOException, JSONException{
		donationSummary.setTodayMaxDonation("Coming Soon");
		donationSummary.setYesterdayMaxDonation("Coming Soon");
	}
	private void loadTotalDonors(HttpClient httpClient,DonationSummary donationSummary) throws ClientProtocolException, IOException, JSONException{
		donationSummary.setTotalNoOfDonorsToday("Coming Soon");
		donationSummary.setTotalNoOfDonorsYesterday("Coming Soon");
		donationSummary.setOverallTotalNoOfDonorsToday("Coming Soon");
		donationSummary.setOverallTotalNoOfDonorsYesterday("Coming Soon");
	}
	private String getResponse(HttpClient httpClient, String url) throws ClientProtocolException, IOException{
		logger.info("Hitting Url = {}", url);
		HttpGet httpGet = new HttpGet(url);
		HttpResponse httpResponse = httpClient.execute(httpGet);
		//System.out.println("Got Response= "+ httpResponse);
		HttpEntity httpEntity = httpResponse.getEntity();
		///System.out.println("Converting to String= "+ httpEntity);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		//System.out.println("IOUtils.copy(httpEntity.getContent(), byteArrayOutputStream);");
		IOUtils.copy(httpEntity.getContent(), byteArrayOutputStream);
		String dayDonationString = byteArrayOutputStream.toString();
		return dayDonationString;
	}
	
}
